package com.gym.myshop.web.controller;

import com.gym.myshop.common.Const;
import com.gym.myshop.entity.Address;
import com.gym.myshop.entity.User;
import com.gym.myshop.services.AddressSerives;
import com.gym.myshop.services.impl.AddressSerivesImpl;
import com.gym.myshop.util.StrUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author ming
 * 2021/11/7
 * description:收获地址
 */
@WebServlet("/addr")
public class AddressController extends BaseServlet {
    private AddressSerives as = new AddressSerivesImpl();

    /**
     *@author:ming
     *@Date: 2021/11/7
     *@description:地址显示，降序排序 默认地址为排在第一
     * uid用户实体的主键属性 aid地址实体的唯一主键列
     */
    public String getUserAddress(HttpServletRequest request, HttpServletResponse response) {
        //登录信息验证
        User user = (User) request.getSession().getAttribute(Const.UserConst.IS_LOGIN);
        if (user == null) {
            return Const.REDIRECT + "login.jsp";
        }
        //访问业务层,重定向转发到地址页面
        List<Address> al = as.getUserAddreeByUid(user.getUid());
        request.setAttribute("addList", al);
        return Const.FORWARD + "self_info.jsp";
    }

    /**
     *@author:ming
     *@Date: 2021/11/7
     *@description:删除地址 aid
     * uid用户实体的主键属性 aid地址实体的唯一主键列
     */
    public String delAdd(HttpServletRequest request, HttpServletResponse response) {
        Integer aid = StrUtils.toInt(request.getParameter("aid"));
        boolean ans = as.delAddress(aid);
        return Const.REDIRECT + "addr?method=getUserAddress";
    }

    /**
     *@author:ming
     *@Date: 2021/11/7
     *@description:添加地址
     * uid用户实体的主键属性 aid地址实体的唯一主键列
     */
    public String insertAddress(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        //登录信息验证
        User user = (User) request.getSession().getAttribute(Const.UserConst.IS_LOGIN);
        //对request.getParameterMap()的返回值使用泛型时是Map<String,String[]>形式v
        Map<String, String[]> parameterMap = request.getParameterMap();
        Address a = new Address();
        //使用工具帮助我们进行对象数据的封装
        BeanUtils.populate(a, parameterMap);
        //设值用户的uid
        a.setUid(user.getUid());
        //业务层添加成功
        boolean ans = as.insertAddress(a);
        return Const.REDIRECT + "addr?method=getUserAddress";
    }

    /**
     *@author:ming
     *@Date: 2021/11/7
     *@description:修改地址
     * uid用户实体的主键属性 aid地址实体的唯一主键列
     */
    public String updateAddr(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        //登录信息验证
        User user = (User) request.getSession().getAttribute(Const.UserConst.IS_LOGIN);
        //对request.getParameterMap()的返回值使用泛型时是Map<String,String[]>形式v
        Map<String, String[]> parameterMap = request.getParameterMap();
        Address a = new Address();
        //使用工具帮助我们进行对象数据的封装
        BeanUtils.populate(a, parameterMap);
        //设值用户的uid
        a.setUid(user.getUid());
        //业务层修改成功
        boolean ans = as.updateAddress(a);
        return Const.REDIRECT + "addr?method=getUserAddress";
    }

    /**
     *@author:ming
     *@Date: 2021/11/7
     *@description:设置默认地址为1，非默认astate = 0
     * uid用户实体的主键属性 aid地址实体的唯一主键列
     */
    public String setDefault(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        Integer aid = StrUtils.toInt(request.getParameter("aid"));
        User user = (User) request.getSession().getAttribute(Const.UserConst.IS_LOGIN);
        boolean ans = as.setDeafult(aid, user.getUid());
        return Const.REDIRECT + "addr?method=getUserAddress";
    }

}
